package tweeks.management.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tweeks.menu.domain.Menu;
import tweeks.menu.service.MenuService;

@Controller
@RequestMapping(value="/management/menu")
public class MenuManagementController {
	
	@Resource(name="MenuServiceImpl")
	private MenuService menuService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String view(Menu menu, ModelMap model) throws IOException {
		
		List<Menu> menuList = menuService.generateMenuList(menu);
		Menu menuTree = menuService.generateMenuTree(menu);
		
		model.addAttribute("menu", menu);
		model.addAttribute("menuList", menuList);
		model.addAttribute("menuTree", menuTree);
		
		return "management/menu";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/create")
	public String create(Menu menu, ModelMap model) throws IOException {
		
		menuService.createMenu(menu);
		
		return "redirect:/management/menu";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/delete")
	public String delete(Menu menu, ModelMap model) throws IOException {
		
		menuService.deleteMenu(menu);
		
		return "redirect:/management/menu";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/modify")
	public String modify(Menu menu, ModelMap model) throws IOException {
		
		menuService.modifyMenu(menu);
		
		return "redirect:/management/menu";
	}
	
}